跳到主要内容

从零开始构建 Linux 开发环境

我的 Linux 配置

一些常用文件的默认位置

zsh 配置文件:~/.zshrc

vscode server:~/.vscode-server

客户端配置 SSH 免密登录

在客户端 .ssh 目录中,打开 bash 配置私钥

ssh-keygen

将客户端的公钥放到服务端上

cat ~/.ssh/id_rsa.pub | ssh username@ip 'cat >> ~/.ssh/authorized_keys'

⭐ 用 ssh-copy-id 命令更方便(仅限 bash)

ssh-copy-id username@ip 

此时,客户端的公钥会被添加到远程服务器上的 ~/.ssh/authorized_keys 文件中

然后就可以使用私钥登录啦~

ssh username@ip [-p port] -i id_rsa

Windows Terminal 配置

字体选择

https://www.nerdfonts.com/font-downloads 随便挑

推荐:

  • CaskaydiaCove Nerd Font Mono(或者叫 CaskaydiaCove NFM)
  • Hack Nerd Font,选 HackNerdFontMono-Regular.ttf

网络配置

镜像源

可选,未经验证!

备份原有软件源文件

sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak_yyyymmdd

修改 sources.list 文件

# 修改为如下地址:
sudo vi /etc/apt/sources.list
# 163源
deb http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ bionic-backports main restricted universe multiverse

更新系统软件源

# 执行命令,更新系统软件源地址:
sudo apt-get update
sudo apt-get upgrade
# 安装lrzsz tree
sudo apt-get -y install lrzsz tree
# 安装 build-essential 软件包集合 (其中就包括 gcc、G ++ 和 make 等)
sudo apt install -y build-essential
# 安装其他东东
sudo apt install libyaml-dev

代理(适用于 WSL)

在 Windows 中的 C:\Users<your_username> 目录下创建一个 .wslconfig 文件,然后在文件中写入如下内容

[experimental]
autoMemoryReclaim=gradual
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

然后用 wsl --shutdown 关闭 WSL,之后再重启

zsh 配置

更改默认 shell 为 zsh

# 查看系统当前使用的shell
echo $SHELL
# 如果没有zsh,那就下一个!
sudo apt-get install -y zsh
# 设置默认zsh
chsh -s /bin/zsh

oh-my-zsh 配置

安装 oh-my-zsh

https://github.com/ohmyzsh/ohmyzsh/wiki

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

安装 Powerlevel10k 主题

https://github.com/romkatv/powerlevel10k#installation

git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/.opt/powerlevel10k
echo 'source ~/.opt/powerlevel10k/powerlevel10k.zsh-theme' >>~/.zshrc

source ~/.zshrc,就会进入 P10k 的配置界面

若要重置 P10k 配置,运行 p10k configure 或者直接修改 ~/.p10k.zsh

这是手动安装方案,所以不需要在 ~/.zshrc 里配置 ZSH_THEME="powerlevel9k/powerlevel9k" 这句话

更改配置文件 .zshrc

vi ~/.zshrc

内容如下:

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
export PATH=$PATH:~/.local/bin/:/snap/bin

# ZSH_THEME="random"

plugins=(
git
extract
rand-quote
themes
z
per-directory-history
history-substring-search
command-not-found
safe-paste
colored-man-pages
sudo
zsh-autosuggestions
# history
zsh-syntax-highlighting
copypath
copyfile
you-should-use
)

source $ZSH/oh-my-zsh.sh

alias :vrc="vi ~/.zshrc"
alias :src="source ~/.zshrc"
alias -s ttt='tar zxvf'
alias cp="cp -i" # 防止 copy 的时候覆盖已存在的文件, 带上 i 选项,文件已存在的时候,会提示,需要确认才能 copy
alias gcm='git checkout master'

# 以自己输入的所有内容为前缀,进行历史查找
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down

# 让 history 命令显示时间
HIST_STAMPS="yyyy-mm-dd"

bindkey '^H' backward-kill-word

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

source ~/.opt/powerlevel10k/powerlevel10k.zsh-theme

此配置文件会根据文档的进展而逐渐完善

一些插件的下载

zsh-autosuggestions

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

zsh-syntax-highlighting

sudo apt install zsh-syntax-highlighting
cd ~/.opt

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git $ZSH_CUSTOM/plugins/zsh-syntax-highlighting

zsh-history-substring-search

 git clone https://github.com/zsh-users/zsh-history-substring-search ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-history-substring-search

zsh-you-should-use

git clone https://github.com/MichaelAquilina/zsh-you-should-use.git $ZSH_CUSTOM/plugins/you-should-use

git 配置

ssh-keygen 一下叭

cd ~/.ssh
ssh-keygen -t rsa

id_rsa.pub 公钥内容,添加到 github 之类的托管平台上

设置名字,邮箱

git config --global user.name "Your Name"
git config --global user.email "email@example.com"

设置其他配置项

git config --global core.editor vi
git config --global credential.helper store

Vim 配置

安装 NeoVim

sudo snap install --beta nvim --classic

或者这样

# 不要直接用 apt install neovim,版本很老!
sudo add-apt-repository ppa:neovim-ppa/unstable
sudo apt-get update
sudo apt-get install neovim

~/.zshrc 中添加别名,以替换默认的 vim 命令

alias vim='nvim'
alias vi='nvim'

使用个人配置(基于 LazyVim)

git clone git@github.com:Trouvaille0198/neovim-config.git ~/.config/nvim
rm -rf ~/.config/nvim/.git

运行 nvim,等待插件下载完成

添加 snippets

工具安装

snap

包管理器,你总会用到的

sudo apt install snapd -y

deb-get

包管理器,你总会用到的

curl -sL https://raw.githubusercontent.com/wimpysworld/deb-get/main/deb-get | sudo -E bash -s install deb-get

bat

cat 的美化替代品

sudo apt install -y bat

使用软链接

mkdir -p ~/.local/bin
ln -s /usr/bin/batcat ~/.local/bin/bat

==一切的软链接都装在 ~/.local==

并在配置中添加

alias cat='batcat'

eza

ls 的替代品

sudo apt install -y eza

在配置中添加

alias ls='eza'

fd

项目地址:https://github.com/sharkdp/fd

一个命令行搜索工具

sudo apt install fd-find

使用软链接

ln -s $(which fdfind) ~/.local/bin/fd

bashtop

https://github.com/aristocratos/bashtop

top 的替代品

sudo add-apt-repository ppa:bashtop-monitor/bashtop
sudo apt update
sudo apt install bashtop

ifconfig

sudo apt install net-tools

cheat

一本常用命令说明书:https://github.com/cheat/cheat

# cd /tmp \
# && wget https://github.com/cheat/cheat/releases/download/4.4.0/cheat-linux-amd64.gz \
# && gunzip cheat-linux-amd64.gz \
# && chmod +x cheat-linux-amd64 \
# && sudo mv cheat-linux-amd64 /usr/local/bin/cheat

cd ~/.opt \
&& wget https://github.com/cheat/cheat/releases/download/4.4.0/cheat-linux-amd64.gz \
&& gunzip cheat-linux-amd64.gz \
&& chmod +x cheat-linux-amd64 \
&& ln -s ~/.opt/cheat-linux-amd64 ~/.local/bin/cheat

👆 最新版本号随时从官方仓库看

dust

GitHub - bootandy/dust: A more intuitive version of du in rust

du 替代

sudo snap install dust

duf

GitHub - muesli/duf: Disk Usage/Free Utility - a better 'df' alternative

df 替代

sudo apt install duf

gping

GitHub - orf/gping: Ping, but with a graph

gui ping

sudo snap install gping

procs

GitHub - dalance/procs: A modern replacement for ps written in Rust

ps 替代

sudo snap install procs

httpie

Snapcraft (Linux) - HTTPie 3.2.2 (latest) docs

curl 替代

sudo snap install httpie

curlie

GitHub - rs/curlie: The power of curl, the ease of use of httpie.

又一个 curl 的替代,保留了 curl 的语法

curl -sS https://webinstall.dev/curlie | bash

其他

sudo apt install unzip
sudo apt install build-essential # gcc etc
sudo apt install libedit-dev liblzma-dev libreadline-dev libffi-dev
sudo apt install zlib1g zlib1g-dev libssl-dev libbz2-dev libsqlite3-dev

编程环境配置

Python

安装 pyenv

https://github.com/pyenv/pyenv#installation

curl https://pyenv.run | bash
# 或者去文档选用其他方法

添加环境变量

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

sudo ln -s ~/.pyenv/bin/pyenv ~/.local/bin/pyenv

安装 / 卸载任意版本

pyenv install --list # 列出所有版本
pyenv install <version-name>
pyenv uninstall <version-name>

如果下得太慢,就用镜像把源码下到缓存目录:

export v=3.12.8; wget https://npm.taobao.org/mirrors/python/$v/Python-$v.tar.xz -P ~/.pyenv/cache/; pyenv install $v

安装 pyenv-virtualenvwrapper

GitHub - pyenv/pyenv-virtualenvwrapper: an alternative approach to manage virtualenvs from pyenv.

这样就能在 pyenv 中使用 [[virtualenv]] 啦,依赖分离

git clone https://github.com/pyenv/pyenv-virtualenvwrapper.git $(pyenv root)/plugins/pyenv-virtualenvwrapper

~/.zshrc 中补充指令缩写

alias pv='pyenv virtualenvwrapper'

之后就能在 pyenv 当前版本的 Python 中使用诸如 mkvirtualenv 或 workon 的指令啦

Go

安装

官网下载:https://golang.org/dl/

下载 go 源码包

版本随时从官网取,确认当前 linux 系统版本是 32 位还是 64 位,再选择 go 源码包

# 查看linux多少位
uname -m
# x86_64

之后所有的软件都安装在 ~/.opt/(而非 /opt/,主要考虑到用户隔离)

cd ~/.opt
sudo wget https://golang.google.cn/dl/go1.21.6.linux-amd64.tar.gz
sudo tar -zxvf go1.21.6.linux-amd64.tar.gz

给予权限

mkdir gocode
sudo chmod -R 777 go/
sudo chmod -R 777 gocode/

配置环境变量

配置 go 的工作空间(配置 GOPATH),以及 go 的环境变量

创建 ~/.opt/gocode/{src,bin,pkg},用于设置 GOPATH 为 ~/.opt/godocer

mkdir -p ~/.opt/gocode/{src,bin,pkg}

~/.opt/gocode/
├── bin
├── pkg
└── src
设置 GOPATH 环境变量

~/zshrc 中写入 GOPATH 信息以及 go sdk 路径

export GOROOT=~/.opt/go           # Golang 源代码目录,安装目录
export GOPATH=~/.opt/gocode # Golang 项目代码目录
export GOBIN=$GOPATH/bin # go install 后生成的可执行命令存放路径

export PATH=$GOROOT/bin:$GOBIN:$PATH # Linux 环境变量
go install 配置代理(可选)
go env -w GOPROXY=https://goproxy.cn

Node.js

官网下载:https://nodejs.org/en/download/

都给我装在 ~/.opt/

下载、解压 Node.js Linux 64 位二进制安装包

cd ~/.opt
wget https://nodejs.org/dist/v22.1.0/node-v22.1.0-linux-x64.tar.xz
tar xvf node-v22.1.0-linux-x64.tar.xz

👆 版本随时从官网取

创建软链接

sudo ln -s ~/.opt/node-v22.1.0-linux-x64/bin/node ~/.local/bin/node
sudo ln -s ~/.opt/node-v22.1.0-linux-x64/bin/npm ~/.local/bin/npm
sudo ln -s ~/.opt/node-v22.1.0-linux-x64/bin/npx ~/.local/bin/npx

Docker

如果是 wsl 的话,直接下个 Docker Desktop 就行

由于 apt 源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。

sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release

为了确认所下载软件包的合法性,需要添加软件源的 GPG 密钥。

curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg


# 官方源
# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

然后,我们需要向 sources.list 中添加 Docker 软件源

echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


# 官方源
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

以上命令会添加稳定版本的 Docker APT 镜像源,如果需要测试版本的 Docker 请将 stable 改为 test。

更新 apt 软件包缓存,并安装 docker-ce

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

安装 docker-compose(最新版本去 仓库 看)

sudo curl -L "https://github.com/docker/compose/releases/download/v2.32.4/docker-compose-$(uname -s)-$(uname -m)" -o ~/.local/bin/docker-compose
sudo chmod +x ~/.local/bin/docker-compose

修改设置(限制日志容量上限、启用 IPv6 等)

cat > /etc/docker/daemon.json <<EOF
{
"log-driver": "json-file",
"log-opts": {
"max-size": "20m",
"max-file": "3"
},
"ipv6": true,
"fixed-cidr-v6": "fd00:dead:beef:c0::/80",
"experimental":true,
"ip6tables":true
}
EOF

openvpn 配置

https://openvpn.net/cloud-docs/tutorials/configuration-tutorials/connectors/operating-systems/linux/tutorial--learn-to-install-and-control-the-openvpn-3-client.html

(可选)设置 swap 分区

查看 Linux 当前分区情况:

free -m

如果是增加 swap 分区,则先把当前所有分区都关闭了:

swapoff -a

创建要作为 Swap 分区文件(其中 /var/swapfile 是文件位置,bs*count 是文件大小,例如以下命令就会创建一个 4G 的文件):

dd if=/dev/zero of=/var/swapfile bs=1M count=4096

建立 Swap 的文件系统(格式化为 Swap 分区文件):

mkswap /var/swapfile

启用 Swap 分区:

swapon /var/swapfile

设置开启启动。在 /etc/fstab 文件中加入一行代码:

/var/swapfile swap swap defaults 0 0

Code-Server

可以在浏览器中编辑代码

安装

curl -fsSL https://code-server.dev/install.sh | sh

To have systemd start code-server now and restart on boot:

sudo systemctl enable --now code-server@$USER

Or, if you don't want/need a background service you can run:

code-server

修改配置

vi ~/.config/code-server/config.yaml 

开启

sudo systemctl restart code-server@$USER

查看进程状态

sudo systemctl status --now code-server@$USER

最终的 .zshrc

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# Path to your oh-my-zsh installation.

# ZSH_THEME="random"

plugins=(
git
extract
rand-quote
themes
z
per-directory-history
history-substring-search
command-not-found
safe-paste
colored-man-pages
sudo
zsh-autosuggestions
# history
zsh-syntax-highlighting
copypath
copyfile
you-should-use
)

source ~/.oh-my-zsh/oh-my-zsh.sh

# Golang config
# export GOROOT=/opt/go #Golang源代码目录,安装目录
# export GOPATH=/opt/gocode #Golang项目代码目录
# export GOBIN=$GOPATH/bin #go install后生成的可执行命令存放路径

# export PATH=$GOROOT/bin:$GOBIN:$PATH # Golang 环境变量
# export PATH=~/.local/bin/:$PATH # python 环境变量
export PYTHON_BUILD_MIRROR_URL="https://npm.taobao.org/mirrors/python/" # pip 镜像
export PYTHON_BUILD_MIRROR_URL_SKIP_CHECKSUM=1

export WORKON_HOME=$HOME/.virtualenvs # virtualenvwrapper
export PYENV_VIRTUALENVWRAPPER_PREFER_PYVENV="true" # pyenv-virtualenvwrapper
export HISTFILE=~/.zsh_history
export PATH="$HOME/.local/bin:$HOME/.poetry/bin:$PATH" # poerty
export PATH="$HOME/.skim/bin/sk:$PATH"
export PATH="/snap/bin:$PATH"
export PATH=$GOROOT/bin:$GOBIN:$(npm prefix -g)/bin:$PATH


alias :vrc="vi ~/.zshrc"
alias :src="source ~/.zshrc"
alias -s ttt='tar zxvf'
alias cp="cp -i" # 防止 copy 的时候覆盖已存在的文件, 带上 i 选项,文件已存在的时候,会提示,需要确认才能 copy
alias ls='eza'
alias ll='eza -l'
alias top='bashtop'
alias cat='batcat'
alias du='dust'
alias df='duf'
alias find='fd'
alias ping='gping'
alias ps='procs'
alias curl='curlie'
alias vim='nvim'
alias vi='nvim'
alias pv='pyenv virtualenvwrapper'
alias gl='git log --graph --pretty=oneline --abbrev-commit'
alias ex="explorer.exe" # 使用windows资源管理器打开当前目录

# git alias
alias gcm='git checkout master'
alias gs='git stash'
alias gsp='git stash pop'
alias gpm='git pull upstream master'
alias gl='git log --graph --pretty=oneline --abbrev-commit'


# work
alias runweb='./bin/run_web.sh'
alias runwork='./bin/run_worker.sh'
alias ml="make lint"
alias mf="make format"

bindkey '^H' backward-kill-word
# eval $(thefuck --alias)

# Change ls colours
LS_COLORS="ow=01;36;40" && export LS_COLORS
# make cd use the ls colours
zstyle ':completion:*' list-colors "${(@s.:.)LS_COLORS}"
autoload -Uz compinit
compinit

# 创建插件小部件
zle -N history-substring-search-up
zle -N history-substring-search-down
# 以自己输入的所有内容为前缀,进行历史查找
bindkey '^P' history-substring-search-up
bindkey '^N' history-substring-search-down
bindkey '^H' backward-kill-word # 使用ctrl+backspace来一次删除一个单词
# 让 history 命令显示时间
HIST_STAMPS="yyyy-mm-dd"

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

source ~/.opt/powerlevel10k/powerlevel10k.zsh-theme
source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"